/*
 * @Author: zhangjun 
 * @Date: 2017-06-06 15:14:19 
 * @Last Modified by: zhangjun
 * @Last Modified time: 2017-06-13 18:44:49
 */
import isString from 'lodash/isString';
import isObject from 'lodash/isObject';
import Enum from 'easy-enum.js';

const wrap = (value, root) => {
    if (isString(value)) {
        return `${root}${value}`;
    } else if (isObject(value)) {
        return value.fullPath || `${value.root}${value.path}`;
    }
    return value;
}
const config = {
    apis: {},
    apiRoot: '',
    cdn: '',
    enums: {}
};

const innerApis = {};

config.addApis = map => {
    Object.keys(map).forEach(key => {
        innerApis[key] = map[key];
        config.apis[key] = wrap(innerApis[key], config.apiRoot);
    });
};

config.addEnums = map => {
    Object.keys(map).forEach(key => {
        config.enums[key] = new Enum(map[key]);
    });
};

config.wrapApis = (apis, root) => {
    Object.keys(innerApis).forEach(key => {
        config.apis[key] = wrap(innerApis[key], root);
    });
};

config.addApis(config.apis);
config.setRoot = root => {
    if (process.env.NODE_ENV !== 'production') {
        config.apiRoot = root;
        config.wrapApis(innerApis, config.apiRoot);
    }
};

config.setCDN = cdn => {
    config.wrapApis(innerApis, cdn);
};


export default config;